package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.enums.ResponseCodeEnum;
import com.easychat.entity.enums.UserStatusEnum;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.query.UserInfoQuery;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.exception.BusinessException;
import com.easychat.service.UserInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/8/16 10:28
 **/
@RestController
@RequestMapping("/admin")
public class AdminUserInfoController extends ABaseController{

    @Resource
    private UserInfoService userInfoService;

    @GlobalInterceptor
    @RequestMapping("/loadUser")
    public ResponseVO loadUser(HttpServletRequest request, @NotEmpty String pageNo) {
//        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserInfoQuery userInfoQuery = new UserInfoQuery();
        userInfoQuery.setPageNo(Integer.parseInt(pageNo));
        userInfoQuery.setOrderBy("create_time desc");
        PaginationResultVO<UserInfo> list = userInfoService.findListByPage(userInfoQuery);
        return getSuccessResponseVO(list);
    }

    @GlobalInterceptor
    @RequestMapping("/updateUserStatus")
    public ResponseVO updateUserStatus(HttpServletRequest request, @NotNull Integer status,
                                       @NotEmpty String userId) {
//        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserStatusEnum userStatusEnum = UserStatusEnum.getByStatus(status);
        if (userStatusEnum == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setStatus(status);
        userInfoService.updateUserInfoByUserId(userInfo, userId);
        return getSuccessResponseVO(null);

    }

    /**
     * 强制下线
     * @param request
     * @param userId
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("/forceOffLine")
    public ResponseVO forceOffLine(HttpServletRequest request,
                                       @NotEmpty String userId) {
        userInfoService.forceOffLine(userId);
        return getSuccessResponseVO(null);
    }

}
